package bs.android.app.component;

import java.util.List;

import bs.android.app.framework.Graphics;
import bs.android.app.framework.Input.TouchEvent;
import bs.android.app.framework.Screen;
import bs.android.app.lichphatsong.constants.Asset;
import bs.android.app.lichphatsong.sub.Event;
import bs.android.app.lichphatsong.sub.ScrollMe;

public class CPopup {	
	final int FONT_SIZE = 20; 
	public int iX, iY, iW, iH;
	public String[][]lstValue;
	public boolean isShowPopup, isActive;
	public int iIndex;
	
	
	//scroll
	public int ifocus = -1;
	public ScrollMe list;
	public Event eMain = null;
	
	//shop popup
	int iTopPopup, KC, iHeightPopup;
	public long ltimeStart,holdTime;
	public int xDown, yDown;
	public boolean isChoose;
	public int iTimeChoose;
	//show popup
	CTextView txtView = null;
//	boolean isRoll;
//	private int iMaxLenRoll;
//	private int iXTitle;
//	int iDelay;
//	int iSign ;
	public CPopup(int x, int y, int w, int h, String[][] value){
		this.iX = x;
		this.iY = y;
		this.iW = w;
		this.iH = h;
		this.lstValue = new String[value.length][2];
		this.lstValue = value; 
		this.isShowPopup = false;
		this.isActive = false;			
		iIndex = 0;
		iTimeChoose = 0;
		list = new ScrollMe(0, 0, 5);
		list.setList(eMain = new Event(){	
			public void action() {				
			}
		});		
		KC = (Screen.iWidthScreen - Asset.imgBgMiddle.getWidth()) / 2;
		list.viewW =  Asset.imgBgMiddle.getWidth();	
		
		iHeightPopup = value.length* (Asset.imgSelect.getHeight()+1);		
		if(iHeightPopup > Screen.iHeightScreen - Asset.imgBgTop.getHeight() - Asset.imgBgBotton.getHeight() - 10*2 - 40){
			iHeightPopup = Screen.iHeightScreen - Asset.imgBgTop.getHeight() - Asset.imgBgBotton.getHeight() - 10*2 - 40;
			iTopPopup = 10;
		}else{			
			iTopPopup = (Screen.iHeightScreen -iHeightPopup - 10 * 2 )/2;
		}		
		list.setViewer(KC, iTopPopup, Asset.imgBgMiddle.getWidth(), iHeightPopup);	
		list.viewH = this.lstValue.length* (Asset.imgSelect.getHeight()+1);
		checkRoll();
	}
	
	public void setListValue(String [][]arr){
		this.lstValue = null;
		this.lstValue = new String[arr.length][2];
		this.lstValue = arr; 
		list.viewH = this.lstValue.length* (Asset.imgSelect.getHeight()+1);
		if(iIndex >=lstValue.length)
			setIndex(lstValue.length -1);
	}
	
	public void setIndex(int index){
		this.iIndex = index;
		checkRoll();
	}
	
	public void setActive(boolean active){
		this.isActive = active;
	}
	
	public void present(Graphics g){
		if(isActive)
			g.drawPixmap(Asset.imgActive, iX, iY, Graphics.ANCHOR_LEFT_TOP);
		else
			g.drawPixmap(Asset.imgNormal, iX, iY, Graphics.ANCHOR_LEFT_TOP);
		if(!isShowPopup){						
			if(txtView != null) txtView.present(g);			
		}else{
			g.drawPixmap(Asset.imgBgTop, KC, iTopPopup, Graphics.ANCHOR_LEFT_TOP);
			int i = 0;
			for(i = 0; i < iHeightPopup ; i+= Asset.imgBgMiddle.getHeight()){
				g.drawPixmap(Asset.imgBgMiddle, KC, iTopPopup + Asset.imgBgTop.getHeight() + i, Graphics.ANCHOR_LEFT_TOP);
			}			
			g.drawPixmap(Asset.imgBgBotton, KC, iTopPopup +  Asset.imgBgTop.getHeight() + i, Graphics.ANCHOR_LEFT_TOP);
			g.setClip(KC, iTopPopup + Asset.imgBgTop.getHeight(), Asset.imgBgTop.getWidth(), iHeightPopup);
			for(int j = 0, top = list.getY();j < lstValue.length; j++, top+=(Asset.imgSelect.getHeight()+1)){				
				g.drawPixmap((ifocus != -1 && j == ifocus)? Asset.imgSelected : Asset.imgSelect, KC+ 7, Asset.imgBgTop.getHeight() + top, Graphics.ANCHOR_LEFT_TOP);
				g.drawString(lstValue[j][0], KC + 80, Asset.imgBgTop.getHeight() + top + 16, (ifocus != -1 && j == ifocus)?0xfffef200 : 0xff1e60b4, Graphics.ANCHOR_LEFT_TOP, FONT_SIZE, Graphics.FONT_TYPE_DEFUALT, true);
			}
			g.restoreClip();
		}		
	}
	
	public void checkRoll(){		
		txtView = new CTextView(iX + 125/2 + 7, iY +(Asset.imgActive.getHeight()/2), 125, Asset.imgActive.getHeight()- 10, lstValue[this.iIndex][0], FONT_SIZE, 0xff373737, Graphics.ANCHOR_HCENTER_VCENTER, true);
	}
	public void update(List<TouchEvent> touchEvents ){
		list.update();		
		
		if(isChoose){
			iTimeChoose++;
			if(iTimeChoose > 10){
				if(ifocus != -1){
					iIndex = ifocus;
					checkRoll();
				}
				iTimeChoose =0;
				isShowPopup = false;
				isActive = false;
				isChoose = false;
			}
		}
		for (int i = 0; i < touchEvents.size(); i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if(isShowPopup){					
					if(event.y < iTopPopup || event.y > iTopPopup + iHeightPopup || event.x < KC || event.x > KC+ Asset.imgSelect.getWidth() ){
						isShowPopup = false;
						if(ifocus != -1){
							iIndex = ifocus;
							checkRoll();
						}
						return ;
					}
					int dx = (event.x - xDown); if(dx < 0) dx = -dx; // <=> int m = dx >> 31; dx = (dx + m) ^ m;
					int dy = (event.y - yDown); if(dy < 0) dy = -dy;					
					holdTime = (int)(System.currentTimeMillis() - ltimeStart);					
					if(dx > 1 || dy > 1){
						list.moveFinish(0, event.x, event.y, (int)holdTime, xDown, yDown);
					}else{
						event.y = list.gotYin(event.y) - 3;
						int index = event.y/(Asset.imgSelect.getHeight()+1);
						if(index < 0 || index >= lstValue.length){
							ifocus = -1;
							return;
						}else{
							this.ifocus = index;
							isChoose = true;
						}
					}					
				}else{
					if(event.x >= iX & event.x <= iX + iW && event.y >= iY && event.y <= iY+ iH){
						System.out.println("isShow Popup ");
						isShowPopup = true;
					}
				}
			}else if(event.type == TouchEvent.TOUCH_DOWN || event.type == TouchEvent.TOUCH_DRAGGED){
				if(isShowPopup){
					if(event.type == TouchEvent.TOUCH_DOWN ){
						xDown =  event.x;
						yDown =  event.y;
						ltimeStart = System.currentTimeMillis();
						list.moveStart(event.x, event.y);					
					}else if(event.type == TouchEvent.TOUCH_DRAGGED){
						isChoose = false;
						list.move(event.x, event.y);						
					}
				}else{
					if(event.x >= iX & event.x <= iX + iW && event.y >= iY && event.y <= iY+ iH){
						isActive = true;
					}else{
						isActive = false;
					}
				}
			}			
		}
	}
	
	public void dispose(){
	}
}
